package gui;

import gui.ObjectInfo;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import lookandfeel.colorScheme;

public class Menu 
{
	private JMenuBar mnb_Menu;
    private ObjectInfo objInfo;
	
	public JMenuBar getMenu()
	{
	    return mnb_Menu;
	}
	public void setObjectinfo(ObjectInfo oi){
		objInfo = oi;
	}
	public Menu()
	{
		mnb_Menu = new JMenuBar();
		
		// Menu items onclick events		
		class menuOnClick implements ActionListener
		{
			int m = 0;
			
			public menuOnClick(int menuID){
				m = menuID;
			}
			public void actionPerformed(ActionEvent event) {
				objInfo.setSelectedMenu(m);
				objInfo.refreshObjectInfo();
			}
		}
		
		// Create string array of menu items
		String[] Menus = {"System", "View", "Objectinfo", "Zoom in", "Zoom out", "Center"};

		// For each string in the array make a new menu item and add this to the menu
		for (int i = 0; i < Menus.length; ++i)
		{
			// Create the menu item			
			final JMenuItem menuItem = new JMenuItem(Menus[i]);
			final colorScheme lookAndFeel = colorScheme.getColorScheme();
			
			// Add hover effect 
			menuItem.addMouseListener(new MouseAdapter() {
				
				// Mouse enter set background
				public void mouseEntered(MouseEvent arg0){
					menuItem.setBackground(lookAndFeel.getMenuHover());
				}
				
				// Mouse exit set background to menubar background
				public void mouseExited(MouseEvent arg0)
	            {
					menuItem.setBackground(mnb_Menu.getBackground());
	            }
			});

			// Get the string length
			int strLen = Menus[i].length();
			
			// Maximum width of a menu item is the strlen * 9 (9px per letter) + 15px padding
			int maxWidth = (strLen * 9) + 15;
			
			// Set maximum widht and height for a menu item
			menuItem.setMaximumSize(new Dimension(maxWidth, 50));
			if(Menus[i] == "Zoom in"){
				// Call zoom in function
			}
			else if(Menus[i] == "Zoom out"){
				// Call zoom out function
			}
			else if(Menus[i] == "Center"){
				// Call center function
			}
			else{
			// Add actionlistener for onclick events. Send menuID
				menuItem.addActionListener(new menuOnClick(i));
			}
			
			// Add menu item to menu
			this.mnb_Menu.add(menuItem);
			// Add seperators between menu items
			this.mnb_Menu.add(new JLabel(" | "));
		}
		this.mnb_Menu.setBorderPainted(false);
	}
}
